<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自定义滚动条</title>
    <style>
        #slideBar {
            height: 500px;
            width: 30px;
            border-radius: 15px;
            background-color: #ccc;
            position: relative;
            margin: 30px auto;
        }
        
        #scroll {
            width: 30px;
            height: 100px;
            border-radius: 15px;
            background-color: #f00;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div id="slideBar">
        <div id="scroll"></div>
    </div>
</body>
<script>
    var slideBar = document.getElementById('slideBar');
    var oScroll = document.getElementById('scroll');
    oScroll.onmousedown = function(e) {
        var e = e || window.event;
        var _this = this;
        var y = e.clientY - _this.offsetTop;
        document.onmousemove = function(e) {
            var e = e || window.event;
            var t = e.clientY - y;
            if (t <= 0) t = 0;
            if (t >= slideBar.offsetHeight - oScroll.offsetHeight) t = slideBar.offsetHeight - oScroll.offsetHeight;
            _this.style.top = t + 'px';
        }
        document.onmouseup = function() {
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
</script>

</html>